package com.xu.controller;

import com.xu.enums.CategoryType;
import com.xu.enums.YesOrNo;
import com.xu.pojo.Carousel;
import com.xu.pojo.Category;
import com.xu.pojo.Users;
import com.xu.pojo.bo.UserBO;
import com.xu.pojo.vo.CategoryVO;
import com.xu.pojo.vo.NewItemsVO;
import com.xu.service.CarouselService;
import com.xu.service.CategoryService;
import com.xu.service.UserService;
import com.xu.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author anan
 * @create 2022-01-11 15:27
 */

@RestController
@RequestMapping("index")
@Api(value = "首页", tags = "首页展示的相关接口")
public class IndexController {
    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public JSONResult carousel(){
        List<Carousel> list = new ArrayList<>();
        String carouselStr = redisOperator.get("carousel");
        if(StringUtils.isBlank(carouselStr)){
            list = carouselService.queryAll(YesOrNo.Yes.type);
            redisOperator.set("carousel", JsonUtils.objectToJson(list));
        }
        else {
            list = JsonUtils.jsonToList(redisOperator.get("carousel"), Carousel.class);
        }
        return JSONResult.ok(list);
    }

    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
    @GetMapping("/cats")
    public JSONResult cats(){
        List<Category> categories = categoryService.queryAllRootCategory();
        return JSONResult.ok(categories);
    }

    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public JSONResult subCat(
            @ApiParam(name = "rootCatId", value = "一级分类ID", required = true)
            @PathVariable Integer rootCatId){

        if(rootCatId == null) return JSONResult.errorMsg("没有该类别");
        List<CategoryVO> subCatList = categoryService.getSubCatList(rootCatId);
        return JSONResult.ok(subCatList);
    }

    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public JSONResult sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级分类ID", required = true)
            @PathVariable Integer rootCatId){

        if(rootCatId == null) return JSONResult.errorMsg("分类不存在");
        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
        //System.out.println(list);
        return JSONResult.ok(list);
    }
}
